package chapter08.chapter8_3_ChangeValueToReference.ver02;

import java.util.Dictionary;
import java.util.Hashtable;

public class Customer {

    private final String _name;

    private static Dictionary _instances = new Hashtable();

    // 改为私有
    // 不允许new
    private Customer(String name) {
        _name = name;
    }

    // 增加工厂方法
    // 将方法名 create改为getNamed
    public static Customer getNamed(String name) {
        return (Customer) _instances.get(name);
    }

    public String getName() {
        return _name;
    }

    public static void loadCustomers() {
        new Customer("a").store();
        new Customer("b").store();
        new Customer("c").store();
    }

    public void store() {
        _instances.put(this.getName(), this);
    }
}
